package com.forever7776.life.admin.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.forever7776.life.admin.controller.BaseController;
import com.forever7776.life.core.common.vo.SysFileListVO;
import com.forever7776.life.core.common.vo.SysFileSearchVO;
import com.forever7776.life.core.constants.SystemConstant;
import com.forever7776.life.core.service.qiniu.QiNiuService;
import com.forever7776.life.core.service.sys.ISysFileService;
import com.qiniu.common.QiniuException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import util.ConfigTool;
import util.ResultUtil;

import java.io.IOException;


/**
 * @author kz
 * @date 2017年12月1日14:32:07
 * @desc 七牛上传
 */
@Controller
@RequestMapping(value = "/up")
public class UploadController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private QiNiuService qiNiuService;

    @Autowired
    private ISysFileService iSysFileService;

    @RequestMapping
    public String index() {
        return LIFE_ADMIN_VIEW_PAGE_PATH + "file_index";
    }


    /**
     * 查询用户上传的图片记录
     *
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list(SysFileSearchVO searchVO) {
        JSONObject result = new JSONObject();
        Page<SysFileListVO> p = new Page<SysFileListVO>();
        p.setCurrent(pageNumber);
        Page<SysFileListVO> page = iSysFileService.selectUsersByCondition(p, searchVO);
        result.put("page", page);
        return result;
    }

    /**
     * 用户上传的图片总数
     *
     * @return
     */
    @RequestMapping("/count")
    @ResponseBody
    public JSONObject count() {
        JSONObject result = new JSONObject();
        result.put("count", iSysFileService.selectCount());
        return result;
    }

    /**
     * 移动文件，要求空间在同一账号下
     *
     * @param fileName    源文件名
     * @param newBucket   目标空间名
     * @param newFileName 目标文件名
     * @throws QiniuException
     */
    public void moveQiNiuFile(String fileName, String newBucket, String newFileName) throws QiniuException {
        qiNiuService.moveFile(SystemConstant.QINIU_BUCKET, fileName, newBucket, newFileName, false);
    }

    /**
     * 根据文件名删除七牛对应的文件
     *
     * @param fileName
     */
    public void delQiNiuFile(String fileName) throws QiniuException {
        qiNiuService.delete(SystemConstant.QINIU_BUCKET, fileName);
    }


    /**
     * 图片上传，文件直传
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        qiNiuService.upload(multipartFile);
        return ResultUtil.getSuccessResult("上传成功");
    }


    /**
     * 图片上传，采用webUpload单次上传
     * 转化为byte数组上传
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping(value = "/pic", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject pic(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        return qiNiuService.pic(multipartFile, getUserId());
    }

    /**
     * 图片上传,不压缩图片
     * 返回格式适合于layui上传图片
     * 转化为byte数组上传
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping(value = "/lay", method = RequestMethod.POST)
    @ResponseBody
    public String lay(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        JSONObject result = qiNiuService.pic(multipartFile, getUserId());
        String end = "{\"code\": 0,\"msg\": \"成功\",\"data\": {\"src\": \"" + "http://" + ConfigTool.getProp("qiniu.domain") + "/" + result.get("key") + "\"}}";
        return end;
    }

    /**
     * 图片上传，压缩图片
     * 返回格式适合于layui上传图片
     * 进行切图
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping(value = "/lay2", method = RequestMethod.POST)
    @ResponseBody
    public String lay2(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        JSONObject result = qiNiuService.pic(multipartFile, getUserId());
        String end = "{\"code\": 0,\"msg\": \"成功\",\"data\": {\"src\": \"" + "http://" + ConfigTool.getProp("qiniu.domain") + "/" + result.get("key") + "?imageslim&imageView2/0/w/1000/h/500\"}}";
        return end;
    }
}
